********************************************************************************
**********************************Bivariate analysis****************************
********************************************************************************

*Globals

global pvar 	PB210 PB040* PB150 PE040 PH020 PL080 PY090* PL03*		
global rvar 	RB090 RX010 RB210 RB050
global hvar 	HX060

global ctr 		AT BE DE DK EL ES FI FR IE IT NL PT SE UK			
global year 	10 11 12 13 14 15 16 17

global ses female age hed ib2.catue ib1.hhtype i.year


*Load 2010-2017 dataset

use "t0", clear


*Define analytic sample
est clear
qui reg ub fob $ses
gen n=e(sample)
keep if n==1

*Generate pooled weights
generate pwx=.

foreach y in $ctr {
	foreach x in $year {
		di "Country: `y' Year: `x'"
		count if year==20`x' & country=="`y'"
		scalar s=r(N)
		count if country=="`y'"
		scalar N=r(N)
		replace pwx=(s/N)*RB050 if year==20`x' & country=="`y'"
	}
}

*Estimate bivariate relationship by country, year fixed effects

foreach x in fob tr1 tr2 tr3{
	foreach y in l p u{
		gen `y'_`x'=.
	}
}

foreach CT in $ctr{
	di "`CT'"
	reg ub fob i.year [aw=pwx] if country=="`CT'" 
	matrix A=r(table)
	replace p_fob=100*A[1,1] if country=="`CT'"
	replace l_fob=100*A[5,1] if country=="`CT'"
	replace u_fob=100*A[6,1] if country=="`CT'"
}

foreach CT in $ctr{
	di "`CT'"
	reg ub tr1 tr2 tr3 i.year [aw=pwx] if country=="`CT'" 
	matrix A=r(table)
	foreach x in 1 2 3 {
		replace p_tr`x'=100*A[1,`x'] if country=="`CT'"
		replace l_tr`x'=100*A[5,`x'] if country=="`CT'"
		replace u_tr`x'=100*A[6,`x'] if country=="`CT'"	
	}
}


*Reduce to a country-level dataset

bysort country year: gen cy=_n
keep if cy==1

foreach CT in $ctr{
	di "`CT'"
	sum gen if country=="`CT'" 
	replace gen =r(mean) if country=="`CT'"
}
		
bysort country: gen c=_n
keep if c==1

keep country p_* l_* u_* gen

order country p_* l_* u_* gen

scatter p_tr1 gen, mlab(country) 


**Benefit Generosity Index

	* Foreign-born

		twoway (rspike l_fob u_fob gen, lcolor(gs12)) ///
		(scatter p_fob gen, mcolor(black) msize(small) mlabc(gs8) mlab(country) mlabpos(11)), ///
		ytitle("") ylabel(-50(10)25, valuelabel angle(0) nogrid) ///
		xtitle("") yline(0, lstyle(foreground) lcolor(gs12)) ///
		xlabel(4(1)15) graphregion(color(white)) ysc(titlegap(2)) xsc(titlegap(3)) title("All foreign-born", color(black)) subtitle(" ") ///
		leg(off) saving(bgi-fob, replace) 

	* <5

		twoway (rspike l_tr1 u_tr1 gen, lcolor(gs12)) ///
		(scatter p_tr1 gen, mcolor(black) msize(small) mlabc(gs8) mlab(country) mlabpos(11)), ///
		ytitle("") ylabel(-50(10)25, valuelabel angle(0) nogrid) ///
		xtitle("") yline(0, lstyle(foreground) lcolor(gs12)) ///
		xlabel(4(1)15) graphregion(color(white)) ysc(titlegap(2)) xsc(titlegap(3)) title("<5 years", color(black)) subtitle(" ") ///
		leg(off) saving(bgi-tr1, replace) 
	
	
	*5-10

		twoway (rspike l_tr2 u_tr2 beng, lcolor(gs12)) ///
		(scatter p_tr2 beng, mcolor(black) msize(small) mlabc(gs8) mlab(country) mlabpos(11)), ///
		ytitle("") ylabel(-50(10)25, valuelabel angle(0) nogrid) ///
		xtitle("") yline(0, lstyle(foreground) lcolor(gs12)) ///
		xlabel(4(1)15) graphregion(color(white)) ysc(titlegap(2)) xsc(titlegap(3)) title("5-10 years", color(black)) subtitle(" ") ///
		leg(off) saving(bgi-tr2, replace) 	


	* >10

		twoway (rspike l_tr3 u_tr3 beng, lcolor(gs12)) ///
		(scatter p_tr3 beng, mcolor(black) msize(small) mlabc(gs8) mlab(country) mlabpos(11)), ///
		ytitle("") ylabel(-50(10)25, valuelabel angle(0) nogrid) ///
		xtitle("") yline(0, lstyle(foreground) lcolor(gs12)) ///
		xlabel(4(1)15) graphregion(color(white)) ysc(titlegap(2)) xsc(titlegap(3)) title(">10 years", color(black)) subtitle(" ") ///
		leg(off) saving(bgi-tr3, replace) 	
		

		
		graph combine bgi-fob.gph bgi-tr1.gph bgi-tr2.gph bgi-tr3.gph, ///
		graphregion(color(white)) xsize(6) ysize(6) iscale(*.8) ///
		l1(Percentage point difference with native-born, size(small)) ///
		b1(Unemployment Benefit Generosity, size(small))

		graph export "bivariate.eps", replace
